package com.yc.boot.work.business;

import cn.hutool.core.convert.Convert;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yc.boot.common.model.base.BaseEntity;
import com.yc.boot.work.entity.TestClass;
import com.yc.boot.work.entity.TestClassTeacher;
import com.yc.boot.work.entity.TestTeacher;
import com.yc.boot.work.model.TestClassAndTeacherPart;
import com.yc.boot.work.model.TestClassInfo;
import com.yc.boot.work.model.TestClassTeacherNode;
import com.yc.boot.work.model.TestTeacherName;
import com.yc.boot.work.service.TestClassAndTeacherPartService;
import com.yc.boot.work.service.TestClassService;
import com.yc.boot.work.service.TestClassTeacherService;
import com.yc.boot.work.service.TestTeacherService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * Create with IntelliT IDEA
 *
 * @Author: zhengmingzhe
 * @Date: 2021/12/14/15:08
 * @Description:
 */
@Slf4j
@Service
public class TestClassTeacherBussiness {
    @Resource
    private TestClassTeacherService testClassTeacherService;

    @Resource
    private TestClassService testClassService;

    @Resource
    private TestTeacherService testTeacherService;

    @Resource
    private TestClassAndTeacherPartService testClassAndTeacherPartService;
    /*
     * @Description  获取班级的详细信息
     * @Param
     * @return List<TestClassInfo>
     **/

    public List<TestClassInfo> getClassInfo(){
        List<TestClassInfo> list = new ArrayList<>();
        List<TestClass> testClass= testClassService.list();
        TestClassInfo info =new TestClassInfo();
        for(TestClass tclass :testClass) {
//            List<TestClassTeacher> testClassTeacher = testClassTeacherService.list(new QueryWrapper<TestClassTeacher>()
//                    .lambda().eq(ObjectUtil.isNotNull(tclass.getId()),TestClassTeacher::getClassId,tclass.getId()));
//
//
//
//            List<TestTeacher> testTeacher =testTeacherService.list(new QueryWrapper<TestTeacher>()
//                    .lambda()
//                    .in(ObjectUtil.isNotEmpty(testClassTeacher),TestTeacher::getId,testClassTeacher.stream()
//                    .map(TestClassTeacher::getTeacherId).collect(Collectors.toList()))
//            );
            List<TestTeacher> testTeacher =new ArrayList<>();
             TestClassInfo inf = Convert.convert(TestClassInfo.class,tclass);
             testClassTeacherService.list(new QueryWrapper<TestClassTeacher>().lambda()
                    .eq(TestClassTeacher::getClassId, tclass.getId()))
                    .stream().forEach(n -> testTeacher.add(testTeacherService.list(new QueryWrapper<TestTeacher>()
                    .lambda().eq(TestTeacher::getId, n.getTeacherId())).get(0)));


           inf.setTestTeacher(testTeacher);
            list.add(inf);

        }
        return list;
    }


/*
 * @Description  测试select()获取班级教师部分信息
 * @Param
 * @return
 **/
    public List<TestClassInfo> getClassAndTeacher(){

        List<TestClassInfo> list = new ArrayList<>();
        List<TestClass> testClass= testClassService.list(new QueryWrapper<TestClass>().lambda()
            .select(BaseEntity::getId,TestClass::getName));
        TestClassInfo info =new TestClassInfo();
        for(TestClass tclass :testClass) {
//            List<TestClassTeacher> testClassTeacher = testClassTeacherService.list(new QueryWrapper<TestClassTeacher>()
//                    .lambda().eq(ObjectUtil.isNotNull(tclass.getId()),TestClassTeacher::getClassId,tclass.getId()));
//
//
//
//            List<TestTeacher> testTeacher =testTeacherService.list(new QueryWrapper<TestTeacher>()
//                    .lambda()
//                    .in(ObjectUtil.isNotEmpty(testClassTeacher),TestTeacher::getId,testClassTeacher.stream()
//                    .map(TestClassTeacher::getTeacherId).collect(Collectors.toList()))
//            );
            List<TestTeacher> testTeacher =new ArrayList<>();
            TestClassInfo inf = Convert.convert(TestClassInfo.class,tclass);
            testClassTeacherService.list(new QueryWrapper<TestClassTeacher>().lambda()
                    .eq(TestClassTeacher::getClassId, tclass.getId()))
                    .stream().forEach(n -> testTeacher.add(testTeacherService.list(new QueryWrapper<TestTeacher>()
                    .lambda().eq(TestTeacher::getId, n.getTeacherId())).get(0)));


            inf.setTestTeacher(testTeacher);
            list.add(inf);

        }
        return list;
    }


    /*
     * @Description 获取班级教师部分信息
     * @Param
     * @return
     **/
    public List<TestClassAndTeacherPart> getPartOfClassAndTeacher(){
        List<TestClass> testClass= testClassService.list();
//        List<TestClassAndTeacherPart> partOfClassAndTeacher = new ArrayList<>();
        List<TestClassAndTeacherPart> partOfClass =Convert.toList(TestClassAndTeacherPart.class,testClass);
        List<TestClassAndTeacherPart> partz =new ArrayList<>();
        for(TestClassAndTeacherPart part :partOfClass){
            List<TestTeacherName> testTeacherNames =new ArrayList<>();
            TestClassAndTeacherPart classAndTeacherPart =new TestClassAndTeacherPart();
               testClassTeacherService.list(new QueryWrapper<TestClassTeacher>().lambda()
                   .eq(TestClassTeacher::getClassId,part.getId()))
               .forEach(n -> testTeacherNames.add(Convert.convert(TestTeacherName.class,testTeacherService.list(new QueryWrapper<TestTeacher>().lambda()
                       .eq(TestTeacher::getId,n.getTeacherId())).get(0))));
            part.setTeacher(testTeacherNames);
            partz.add(part);
        }
        return partz;
    }

    /*
     * @Description
     * @Param
     * @return
     **/
    public List<TestClassTeacherNode> queryPartOfClassAndTeachers(){
        List<TestClassTeacherNode> part=Convert.toList(TestClassTeacherNode.class,testClassAndTeacherPartService.queryClassAndTeacher(new LambdaQueryWrapper<TestClassTeacherNode>()));

        return part;

    }

}
